package com.socket.handler;

import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import io.netty.buffer.ByteBufUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialPortHandler implements SerialPortEventListener {
    private final SerialPort serialPort;

    public SerialPortHandler(SerialPort serialPort) {
        this.serialPort = serialPort;
    }

    @Override
    public void serialEvent(SerialPortEvent serialPortEvent) {
        int eventType = serialPortEvent.getEventType();
        switch (eventType) {
            // 数据到达
            case SerialPortEvent.DATA_AVAILABLE:
                byte[] byteArray = read(serialPort);
                if (null != byteArray) {
                    System.out.println("byteArray: " + ByteBufUtil.hexDump(byteArray));
                }
                break;
            // 输出缓冲区已清空
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                // 清除发送
            case SerialPortEvent.CTS:
                // 数据设备准备好
            case SerialPortEvent.DSR:
                // 响铃侦测
            case SerialPortEvent.RI:
                // 载波侦测
            case SerialPortEvent.CD:
                // 溢位错误
            case SerialPortEvent.OE:
                // 奇偶校验错误
            case SerialPortEvent.PE:
                // 帧错误
            case SerialPortEvent.FE:
                // 通讯中断
            case SerialPortEvent.BI:
                System.err.println("eventType[" + eventType + "]");
                break;
            default:
                System.err.println("eventType[" + eventType + "] match fail");
        }
    }

    private byte[] read(SerialPort serialPort) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        InputStream inputStream = null;
        try {
            inputStream = serialPort.getInputStream();
            int available = inputStream.available();
            byte[] byteArray = new byte[available];
            while (available > 0) {
                inputStream.read(byteArray);

                available = inputStream.available();
            }

            return byteArray;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public void send(SerialPort serialPort, byte[] byteArray) {
        OutputStream outputStream = null;

        try {
            outputStream = serialPort.getOutputStream();
            outputStream.write(byteArray);
            outputStream.flush();

            System.out.println("send to commPort[" + serialPort.getName() + "]: " + ByteBufUtil.hexDump(byteArray));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
